from fileinput import filename
import os
import sys
import pathlib
import re

if __name__ == "__main__":
    runpath = os.getcwd(    )
    path = pathlib.Path(__file__).parent.absolute()
    allfiles = list()
    for root,dirs,files in os.walk(runpath):
        for file in files:
            if file.endswith(".jpg") or file.endswith(".png") or file.endswith(".jpeg") or file.endswith("webp"):
                fileabspath = pathlib.Path(os.path.join(root, file))
                # 将路径格式为标准格式，方便后续处理
                fileabspath = fileabspath.resolve().__str__()
                allfiles.append(fileabspath)

    mainfiles = list()
    detailfiles = list()
    skufiles = list()
    videofiles = list()
    otherfiles = list()
    
    for file in allfiles:
        group = pathlib.Path(file).resolve().parent.name.lower()
        
        # mainfiles
        if group.find("main") != -1:
            mainfiles.append(file)
        elif group.find("主图") != -1:
            mainfiles.append(file)
        elif group.find("首图") != -1:
            mainfiles.append(file)

        # detailfiles 
        elif group.find("detail") != -1:
            detailfiles.append(file)
        elif group.find("详情") != -1 and group.find("长图") == -1:
            detailfiles.append(file)
            
        #  skufiles 
        elif group.find("sku") != -1:
            skufiles.append(file)
        elif group.find("规格") != -1:
            skufiles.append(file)
        elif group.find("属性") != -1:
            skufiles.append(file)
        
        # otherfiles
        else:
            otherfiles.append(file)
   
        
# 
# 
# 
# 
# 

    
    # sort mainfiles
    mainfiles.sort(key=lambda item: int(re.search(r"\d+", pathlib.Path(item).resolve().stem).group()))
    
    # rename mainfiles
    for index,file in enumerate(iterable=mainfiles,start=1):
        current_index = index
        mainfilelocaltion_directory = pathlib.Path(file).resolve().parent
        mainfilename = pathlib.Path(file).name
        filebasename = pathlib.Path(file).stem
        filesuffixname = pathlib.Path(file).suffix
        
        new_filesuffixname = filesuffixname
        new_filebasename = f"Main-{index:03}-{filebasename}"
        
        new_filebasename.replace("主","-").replace("图","-").replace("首","-")
        
        new_mainfilename = f"{new_filebasename}{new_filesuffixname}"
        print(mainfilename,'\t',new_mainfilename)
        
        
    # sort detailfiles
    detailfiles.sort(key=lambda item: int(re.search(r"\d+", pathlib.Path(item).resolve().stem).group()))
    
    # rename detailfiles
    for index,file in enumerate(iterable=detailfiles,start=1):
        current_index = index
        detailfilelocaltion_directory = pathlib.Path(file).resolve().parent
        detailfilename = pathlib.Path(file).name
        filebasename = pathlib.Path(file).stem
        filesuffixname = pathlib.Path(file).suffix
        
        new_filesuffixname = filesuffixname
        new_filebasename = f"Detail-{index:03}-{filebasename}"
        new_filebasename.replace("详情","-").replace("长图","-").replace("长图","-")
        new_detailfilename = f"{new_filebasename}{new_filesuffixname}"
        
        print(detailfilename,'\t',new_detailfilename)
        
    # sort skufiles
    skufiles.sort(key=lambda item: int(re.search(r"\d+", pathlib.Path(item).resolve().stem).group()))
    for index,file in enumerate(iterable=skufiles,start=1):
        current_index = index
        skufilelocaltion_directory = pathlib.Path(file).resolve().parent
        skufilename = pathlib.Path(file).name
        filebasename = pathlib.Path(file).stem
        filesuffixname = pathlib.Path(file).suffix
        new_filesuffixname = filesuffixname
        new_filebasename = f"SKU-{index:03}-{filebasename}"
        new_filebasename.replace("规格","-").replace("属性","-")
        new_skufilename = f"{new_filebasename}{new_filesuffixname}"
        print(skufilename,'\t',new_skufilename)
        
    # sort otherfiles
    otherfiles.sort(key=lambda item: int(re.search(r"\d+", pathlib.Path(item).resolve().stem).group()))
    for index,file in enumerate(iterable=otherfiles,start=1):
        current_index = index
        otherfilelocaltion_directory = pathlib.Path(file).resolve().parent
        otherfilename = pathlib.Path(file).name
        filebasename = pathlib.Path(file).stem
        filesuffixname = pathlib.Path(file).suffix
        new_filesuffixname = filesuffixname
        new_filebasename = f"Other-{index:03}-{filebasename}"
        new_filebasename.replace("其他","-")
        new_otherfilename = f"{new_filebasename}{new_filesuffixname}"
        print(otherfilename,'\t',new_otherfilename)
        
    input()